package 第8场_小白入门赛;

import java.math.BigDecimal;
import java.util.*;
import java.io.*;
public class _03 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st;
		int n = Integer.parseInt(br.readLine());
		BigDecimal b1;
		int b2;
		BigDecimal b3;
		BigDecimal mod = new BigDecimal(10);
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			b1 = new BigDecimal(st.nextToken());
			b2 = Integer.parseInt(st.nextToken());
			b3 = b1.pow(b2).remainder(mod);
			bw.append(b3.toString() + "\n");
		}
		bw.flush();
		bw.close();
		br.close();
	}
}
